package com.bwsoft.portal.action;


import java.io.File;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.bwsoft.admin.domain.Account;
import com.bwsoft.admin.domain.AccountAccess;
import com.bwsoft.admin.domain.Report;
import com.bwsoft.api.pojo.ResponsePojo;
import com.bwsoft.portal.domain.valueObject.ReportVO;


public class ReportAction extends PortalBaseAction {

	private static Logger LOG = Logger.getLogger(ReportAction.class);
	
	private String s_reportNumber;
	private String s_sampleName;
	private int pagecount = 0;
	private int pageSize = 0;
	private Integer compId;
	private String compName;
	private String reportNumber;
	private String sampleName;
	private String scrqph;
	private File report;
	private String reportFileName;
	//private File file;
	//private String fileFileName;
	private File Filedata;
	private String Filename;
	private Boolean canMultiUpload;
	private String browser;

	public String index() {
		
		long recordcount = 0;
		try {
			if (s_sampleName != null && !"".equals(s_sampleName)) {
				s_sampleName = URLDecoder.decode(s_sampleName, "UTF-8");
				s_sampleName = URLDecoder.decode(s_sampleName, "UTF-8");
			}
			recordcount = reportService.getTotalCount(s_reportNumber, s_sampleName, (Account)session.get("Account"));
		} catch (Exception e) {
			LOG.info("查找质量控制出错");
			e.printStackTrace();
		}
		
		getPageInfo(recordcount);
				
		return "reportIndex";
	}
	
	public String getReportJson() {
		List<Report> qcList=null;
		List<ReportVO> qcVOs = null;
			
		try {
			if (s_sampleName != null && !"".equals(s_sampleName)) {
				s_sampleName = URLDecoder.decode(s_sampleName, "UTF-8");
				s_sampleName = URLDecoder.decode(s_sampleName, "UTF-8");
			}
			qcList = reportService.findData(page, rows, s_reportNumber, s_sampleName, (Account)session.get("Account"));
			qcVOs = reportService.getVo(qcList, ((Account)session.get("Account")).getAccountType());
		} catch (Exception e) {
			LOG.info("查找质量控制出错");
			e.printStackTrace();
		}
		
		responseJson.put("rows", qcVOs);
		return "mapJsonResult";
	}
	
	private void getPageInfo(long recordcount) {
		pagecount = (int)(recordcount % getRows() == 0 ? (recordcount / getRows()) : (recordcount / getRows() + 1));
		
		if (pagecount < 1)
		{
			pageSize = 1;
			pagecount = 1;
		}
		else if (pagecount > 8)
			pageSize = 8;
		else
			pageSize = pagecount;
	}

	public String showUpload() {
		return "showUpload";
	}
	
	public String showMultiUpload() {
		canMultiUpload = accountAccessService.getAccess();
		return "showMultiUpload";
	}
	
	public String checkCompName() {
		try {
			if (compName == null || "".equals(compName)) { 
				responseJson.put("result", 1);
				responseJson.put("msg", "企业名称不能为空！");
				return "mapJsonResult";
			} else {
				compName = URLDecoder.decode(compName, "UTF-8");
				compName = URLDecoder.decode(compName, "UTF-8");
			}
			ResponsePojo rp=reportService.checkCompName(compName);
			Map map = new HashMap();
			Account comp = (Account)rp.getData();
			if (comp != null) {
				map.put("compId", comp.getAccountId());
				map.put("compName", comp.getCompName());
			}
			responseJson.put("result", rp.getErrcode());
			responseJson.put("msg", rp.getErrmsg());
			responseJson.put("data", map);
		} catch (Exception e) {
			LOG.info("查找公司名称出错");
			e.printStackTrace();
		}
		return "mapJsonResult";
	}
	
	public String doUpload() {
		ResponsePojo rp = null;
		try {
			if (compName == null || "".equals(compName)) { 
				responseJson.put("result", 1);
				responseJson.put("msg", "企业名称不能为空！");
				return "mapJsonResult";
			} else {
				compName = URLDecoder.decode(compName, "UTF-8");
				compName = URLDecoder.decode(compName, "UTF-8");
			}
			rp=reportService.checkCompName(compName);
			if (rp.getErrcode() != 0) { 
				responseJson.put("result", 2);
				responseJson.put("msg", "公司不存在！");
				return "mapJsonResult";
			}
			if (reportNumber == null || "".equals(reportNumber)) { 
				responseJson.put("result", 3);
				responseJson.put("msg", "报告编号不能为空！");
				return "mapJsonResult";
			}
			if (sampleName == null || "".equals(sampleName)) { 
				responseJson.put("result", 4);
				responseJson.put("msg", "样品名称不能为空！");
				return "mapJsonResult";
			}
			if (scrqph == null || "".equals(scrqph)) { 
				responseJson.put("result", 5);
				responseJson.put("msg", "生产日期批号不能为空！");
				return "mapJsonResult";
			}
			if (report == null || reportFileName == null || "".equals(reportFileName)) { 
				responseJson.put("result", 6);
				responseJson.put("msg", "附件不能为空！");
				return "mapJsonResult";
			}
			String reportFileType = reportFileName.substring(reportFileName.lastIndexOf(".")).toLowerCase();;
			if(reportFileName != null && !"".equals(reportFileName) && !".pdf".equals(reportFileType)){
				responseJson.put("result", 7);
				responseJson.put("msg", "附件不是PDF类型！");
				return "mapJsonResult";
			}
			if(reportFileName != null && !"".equals(reportFileName) && !newsService.isFitSize(report)){
				responseJson.put("result", 8);
				responseJson.put("msg", "附件不能超过8M！");
				return "mapJsonResult";
			}
			Account uploader = (Account)session.get("Account");
			if (uploader.getAccountType() != 2) {
				responseJson.put("result", 9);
				responseJson.put("msg", "只有实验室用户才能上传!");
				return "mapJsonResult";
			}
			rp = reportService.saveReport((Account)rp.getData(), uploader, 
					reportNumber, sampleName, scrqph, report, reportFileName);
		} catch (Exception e) {
			LOG.info("查找公司名称出错");
			e.printStackTrace();
		}
		
		//easyui返回的json的contentType必须为text/html
		response.setContentType("text/html;charset=utf-8");
		JSONObject json = new JSONObject();
		json.put("result", rp.getErrcode());
		json.put("msg", rp.getErrmsg());
		try {
			PrintWriter out = response.getWriter();
			out.write(json.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public String doMultiUpload() {
		/*ResponsePojo rp = null;
		try {
			rp = reportService.saveMultiReport(accountService.findAccount(compId), (Account)session.get("Account"), 
					file, fileFileName);
		} catch (Exception e) {
			LOG.info("上传出错");
			e.printStackTrace();
		}
		//easyui返回的json的contentType必须为text/html，否则ie9上传有问题
		response.setContentType("text/html;charset=utf-8");
		JSONObject json = new JSONObject();
		json.put("result", rp.getErrcode());
		json.put("msg", rp.getErrmsg());
		try {
			PrintWriter out = response.getWriter();
			if (browser != null && "ie".equals(browser)) {
				if (rp.getErrcode() == 0)
					out.write("<script>alert('保存成功');history.back();</script>");
				else
					out.write("<script>alert('保存失败');history.back();</script>");
			}
			else {
				out.write(json.toString());
			}
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;*/
		ResponsePojo rp = null;
		try {
			rp = reportService.saveMultiReport(accountService.findAccount(compId), (Account)session.get("Account"), 
					Filedata, Filename);
		} catch (Exception e) {
			LOG.info("上传出错");
			e.printStackTrace();
		}
		responseJson.put("result", rp.getErrcode());
		responseJson.put("msg", rp.getErrmsg());
		return "mapJsonResult";
	}

	public String getS_reportNumber() {
		return s_reportNumber;
	}

	public void setS_reportNumber(String s_reportNumber) {
		this.s_reportNumber = s_reportNumber;
	}

	public String getS_sampleName() {
		return s_sampleName;
	}

	public void setS_sampleName(String s_sampleName) {
		this.s_sampleName = s_sampleName;
	}

	public int getPagecount() {
		return pagecount;
	}

	public void setPagecount(int pagecount) {
		this.pagecount = pagecount;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getCompName() {
		return compName;
	}

	public void setCompName(String compName) {
		this.compName = compName;
	}

	public Integer getCompId() {
		return compId;
	}

	public void setCompId(Integer compId) {
		this.compId = compId;
	}

	public String getReportNumber() {
		return reportNumber;
	}

	public void setReportNumber(String reportNumber) {
		this.reportNumber = reportNumber;
	}

	public String getSampleName() {
		return sampleName;
	}

	public void setSampleName(String sampleName) {
		this.sampleName = sampleName;
	}

	public String getScrqph() {
		return scrqph;
	}

	public void setScrqph(String scrqph) {
		this.scrqph = scrqph;
	}

	public File getReport() {
		return report;
	}

	public void setReport(File report) {
		this.report = report;
	}

	public String getReportFileName() {
		return reportFileName;
	}

	public void setReportFileName(String reportFileName) {
		this.reportFileName = reportFileName;
	}

	public File getFiledata() {
		return Filedata;
	}

	public void setFiledata(File filedata) {
		Filedata = filedata;
	}

	public String getFilename() {
		return Filename;
	}

	public void setFilename(String filename) {
		Filename = filename;
	}

	public Boolean getCanMultiUpload() {
		return canMultiUpload;
	}

	public void setCanMultiUpload(Boolean canMultiUpload) {
		this.canMultiUpload = canMultiUpload;
	}

	public String getBrowser() {
		return browser;
	}

	public void setBrowser(String browser) {
		this.browser = browser;
	}
}
